<template>
  <!-- 筛选区 -->
  <div class="sub-filter" v-if="filterdata && !isloading">
    <div class="item">
      <div class="head">品牌：</div>
      <div class="body">
        <a
          href="javascript:;"
          v-for="item in filterdata.brands"
          :key="item.id"
          :class="{ active: item.id === filterdata.brands.selected }"
          @click="changebrand(item.id)"
        >
          {{ item.name }}
        </a>
      </div>
    </div>
    <div class="item" v-for="item in filterdata.saleProperties" :key="item.id">
      <div class="head">{{ item.name }}</div>
      <div class="body">
        <a
          href="javascript:;"
          :class="{ active: prop.id === item.properties.selected }"
          v-for="prop in item.properties"
          :key="prop.id"
          @click="changeprops(item.properties, prop.id)"
          >{{ prop.name }}
        </a>
      </div>
    </div>
  </div>
  <div v-else class="sub-filter">
    <XtxSkeleton class="item" width="800px" height="30px" />
    <XtxSkeleton class="item" width="800px" height="30px" />
    <XtxSkeleton class="item" width="600px" height="30px" />
    <XtxSkeleton class="item" width="600px" height="30px" />
    <XtxSkeleton class="item" width="600px" height="30px" />
  </div>
</template>
<script>
import { ref, watch } from 'vue'
import { useRoute } from 'vue-router'
import { findSubCategoryFilter } from '@/apis/category'
export default {
  name: 'SubFilter',
  setup (props, { emit }) {
    const route = useRoute()
    const filterdata = ref(null)
    // 骨架是否显示
    const isloading = ref(false)
    watch(
      () => route.params.id,
      (newid) => {
        if (!newid || `/category/sub/${newid}` !== route.path) return
        const cate = {}
        isloading.value = true
        findSubCategoryFilter(newid).then((res) => {
          res.result.brands.unshift({ id: null, name: '全部' })
          // 添加默认选中的id
          res.result.brands.selected = null
          res.result.saleProperties.forEach((item) => {
            // 添加默认选中的id
            item.properties.selected = null
            item.properties.unshift({ id: null, name: '全部' })
          })
          cate.brands = res.result.brands
          cate.saleProperties = res.result.saleProperties
          filterdata.value = cate
          isloading.value = false
        })
      },
      { immediate: true }
    )

    // 获取过滤的数据参数
    const getfilterparams = () => {
      const obj = { brandId: null, attrs: [] }
      obj.brandId = filterdata.value.brands.selected
      filterdata.value.saleProperties.forEach((sale) => {
        if (!sale.properties.selected) return
        const prop = sale.properties.find(
          (item) => sale.properties.selected === item.id
        )
        obj.attrs.push({ groupName: sale.name, propertyName: prop.name })
      })
      if (obj.attrs.length === 0) obj.attrs = null
      return obj
    }
    // 记录当前选择的品牌
    const changebrand = (id) => {
      if (filterdata.value.brands.selected === id) return
      filterdata.value.brands.selected = id
      emit('filterchange', getfilterparams())
    }
    // 记录选择的销售属性
    const changeprops = (item, id) => {
      if (item.selected === id) return
      item.selected = id
      emit('filterchange', getfilterparams())
    }
    return { filterdata, isloading, changebrand, changeprops }
  }
}
</script>
<style scoped lang="less">
// 筛选区
.sub-filter {
  background: #fff;
  padding: 25px;
  .item {
    display: flex;
    line-height: 40px;
    .head {
      width: 80px;
      color: #999;
    }
    .body {
      flex: 1;
      a {
        margin-right: 36px;
        transition: all 0.3s;
        display: inline-block;
        &.active,
        &:hover {
          color: @xtxColor;
        }
      }
    }
  }
  .xtx-skeleton {
    margin: 20px 0;
  }
}
</style>
